import 'package:myapp/bean/article.dart';
import 'package:myapp/bean/category.dart';
import 'package:myapp/bean/banner.dart';
import 'package:myapp/bean/hotword.dart';
import 'package:myapp/bean/frequently.dart';

import 'http_manager.dart';

class OnlineRepository {
  // 置顶文章
  static Future fetchTopArticles() async {
    var response = await http.get('article/top/json');
    return response.data.map<Article>((item) => Article.fromMap(item)).toList();
  }

  // 文章
  static Future fetchArticles(int pageNum, {int cid}) async {
    // await Future.delayed(Duration(seconds: 1)); //增加动效
    var response = await http.get('article/list/$pageNum/json',
        queryParameters: (cid != null ? {'cid': cid} : null));
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }

  static Future getProjectList(int pageNum) async {
    ///article/listproject/{page}/json
    var response = await http.get('article/listproject/$pageNum/json');
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }

  static Future getUserArticleList(int pageNum) async {
    var response = await http.get('user_article/list/$pageNum/json');
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }

  static Future getProjectCategories() async {
    var response = await http.get('project/tree/json');
    return response.data
        .map<Category>((item) => Category.fromMap(item))
        .toList();
  }

  static Future getProjectListByCid(int pageNum, int cid) async {
    var response = await http.get('project/list/$pageNum/json',
        queryParameters: (cid != null ? {'cid': cid} : null));
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }

  static Future getWechatCategories() async {
    var response = await http.get('wxarticle/chapters/json');
    return response.data
        .map<Category>((item) => Category.fromMap(item))
        .toList();
  }

  static Future getWechatArticleList(int pageNum, int id) async {
    var response = await http.get('wxarticle/list/$id/$pageNum/json');
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }

  static Future getArticleCategories() async {
    var response = await http.get('tree/json');
    return response.data
        .map<Category>((item) => Category.fromMap(item))
        .toList();
  }

  static Future getArticleListByCid(int pageNum, int cid) async {
    var response = await http.get('article/list/$pageNum/json',
        queryParameters: (cid != null ? {'cid': cid} : null));
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }


  static Future getBanners() async {
    var response = await http.get('banner/json');
    return response.data.map<BannerBean>((item) => BannerBean.fromJson(item))
        .toList();
  }

  static Future getHotWords() async {
    var response = await http.get('hotkey/json');
    return response.data.map<HotWord>((item) => HotWord.fromJson(item))
        .toList();
  }

  static Future getFrequentlyWebsites() async {
    var response = await http.get('friend/json');
    return response.data.map<Frequently>((item) => Frequently.fromJson(item))
        .toList();
  }
}
